import authorizeApi from '@/api/AuthorizeApi'
import { treeToList } from '@/core/utils/Common'

const menu = {
  // 默认情况下这些属性都还是全局的，开启命名空间后变为模块的
  // 开启命名空间，这里面的属性和方法，需要用当前名字的前缀才能获取
  namespaced: true,
  state() {
    return {
      active: '/dashboard',
      openMenus: [
        { name: '仪表板', path: '/dashboard', loading: false, closable: false }
      ],
      navMenus: [],
      navList: []
    }
  },
  getters: {
    openMenuTabs() {
      return ''
    }
  },
  mutations: {
    // 设置左侧导航菜单
    setNavMenus(state, payload) {
      state.navMenus = payload
      state.navList = treeToList(payload)
    },
    // 设置当前菜单页
    setActive(state, payload) {
      state.active = payload
    },
    setOpenMenus(state, payload) {
      state.openMenus =payload
    },
    // 打开
    openMenuTab(state, payload) {
      if (state.openMenus.findIndex(f => f.path == payload.path) < 0) {
        state.openMenus.push(payload)
      }
      state.active = payload.path
    },
    // 关闭
    closeMenuTab(state, payload) {
      let idx = state.openMenus.findIndex(f => f.path == payload)
      state.openMenus = state.openMenus.filter(f => f.path !== payload)
      state.active = state.openMenus[idx-1]?.path
    },
    // 激活状态
    enableStatus(state, payload) {
      state.openMenus.forEach(it => {
        if (payload.value == it.path) {
          it.loading = true
        }
      })
    },
    // 禁用状态
    disableStatus(state, payload) {
      state.openMenus.forEach(it => {
        if (payload.value == it.path) {
          it.loading = false
        }
      })
    },
    // 停止loading状态
    stopAllStatus(state) {
      state.openMenus.forEach(it => {
        it.loading = false
      })
    },
    // 重置状态
    resetState(state) {
      state.active = '/dashboard',
      state.openMenus = [ { name: '仪表板', path: '/dashboard', loading: false, closable: false } ],
      state.navMenus = []
      state.navList = []
    }
  },
  actions: {
    // 从服务端获联菜单数据
    initNavMenuData(context) {
      return authorizeApi.getNavMenus().then(res => {
        if (res.code == 0) {
          context.commit('setNavMenus', res.data)
          //sessionStorage.setItem('REMOTE_ROUTERS_DATA', )
          // 调用菜单模块中[menu.js]加载菜单
          context.dispatch('auth/setPermis',  res.permissions , { root: true })
          Promise.resolve('成功')
        } else {
          Promise.reject(res.msg)
        }
      })
    },
    // 打开菜单
    openMenu({ commit, state }, payload) {
      let menu = state.navList.filter(it => it.path == payload)
      if (menu?.length > 0) {
        commit('openMenuTab', menu[0])
      }
    },
    // 关闭所有菜单
    closeAllMenu(context, payload) {
      console.info(payload)
      console.info(context)
    },
    // 关闭菜单
    closeMenu({ commit }, payload) {
      commit('closeMenuTab', payload)
    },
    // 刷新页路由
    refreshMenu({ commit }, payload) {
      commit('enableStatus', payload)
      setTimeout(() => {
        commit('disableStatus', payload)
      }, 1200)
    }
  }
}
export default menu